/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
jQuery.noConflict();
var uname;
var pass;

function createXmlHttpRequestObject() {
    var ro;
    var browser = navigator.appName;
    // Need to determine IE7 and not do this.
    if (browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else {
        ro = new XMLHttpRequest();
    }
    return ro;
}

function createResponseXML(textXML) {
    // code for IE
    if (window.ActiveXObject) {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(textXML);
        return xmlDoc;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        var parser = new DOMParser();
        var xmlDoc = parser.parseFromString(textXML, "text/xml");
        return xmlDoc;
    }
}

function loginUser(username,password){
    var url = '/Alluring_Decors-war/LoginAdminServlet?username=' +username + '&password=' + password;
    alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = finishUpdate;
    aeRequest.send(null);
}

function finishUpdate(){
    if(aeRequest.readyState==4){
        if(aeRequest.status == 200){
            var response = createResponseXML(aeRequest.responseText);
            var result = response.getElementsByTagName("result")[0].firstChild.nodeValue;
            if (result == "success") {
                alert("login success!");
                window.location.href = "admin.jsp";
            }else{
                alert("login failed!");
            }
        }
    }
}

function randomString() {
	var chars = "0123456789";
	var string_length = 6;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
	var rnum = Math.floor(Math.random() * chars.length);
	randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}
String.prototype.trim = function()
{
	var trimmed = this.replace(/^\s+|\s+$/g, '') ;
	return trimmed;
};
jQuery(document).ready(function() {
    jQuery("#btnLog").click(function(){
        uname = jQuery("#login").val();
        pass = jQuery("#password").val();

        if(uname!=null && uname.length>0 && pass != null && pass.length>0){
            loginUser(uname,pass);
        }
    });
});


